package wwl.lsf.hellospringboot.controller;

import com.alibaba.fastjson.JSON;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import wwl.lsf.repository.LetCodeMapper;

@Controller
@Slf4j
@RequestMapping("letcode")
public class LetCodeController {

  @Autowired
  private LetCodeMapper letCodeMapper;

  @GetMapping
  private String view() {
    return "letcode/index";
  }


  @PostMapping
  @ResponseBody
  public String hello(@RequestBody String json) {
    if (StringUtils.isBlank(json)) {
      return "力扣的json为空了";
    }
    StringBuilder result = new StringBuilder();
    Map<String, Object> map = JSON.parseObject(json, Map.class);
    Map<String, List<String>> header = (Map<String, List<String>>) map.get("headers");
    header.forEach((k, v) -> {
      StringBuilder createTable = new StringBuilder();
      StringBuilder dropTable = new StringBuilder("DROP TABLE IF EXISTS ");
      createTable.append("create table ");
      createTable.append(k);
      result.append(k);
      result.append(",");
      dropTable.append(k);
      createTable.append("( ");
      String pk = "";
      for (int i = 0; i < v.size(); i++) {
        createTable.append(v.get(i));
        pk = v.get(0);
        if (i == 0) {
          createTable.append(" int (20) NOT NULL AUTO_INCREMENT, ");
        } else {
          createTable.append(" VARCHAR (20),");
        }
      }
      createTable.append("PRIMARY KEY (");
      createTable.append(pk);
      createTable.append("))");
      //删除表
      letCodeMapper.executeSql(dropTable.toString());
      //建表语句
      letCodeMapper.executeSql(createTable.toString());
      System.out.println(createTable);
    });
    Map<String, List<List<Object>>> rows = (Map<String, List<List<Object>>>) map.get("rows");
    rows.forEach((i, j) -> {
      StringBuilder insertSql = new StringBuilder("INSERT INTO ");
      insertSql.append(i);
      insertSql.append(" VALUES ");
      for (int i1 = 0; i1 < j.size(); i1++) {
        insertSql.append(" ( ");
        for (int i2 = 0; i2 < j.get(i1).size(); i2++) {
          if (j.get(i1).get(i2) == null) {
            insertSql.append("null");
          } else {
            insertSql.append("'");
            insertSql.append(j.get(i1).get(i2));
            insertSql.append("'");
          }
          if (i2 != j.get(i1).size() - 1) {
            insertSql.append(",");
          }
        }
        if (i1 != j.size() - 1) {
          insertSql.append(" ), ");
        } else {
          insertSql.append(" ) ");
        }
      }
      letCodeMapper.executeSql(insertSql.toString());
      log.warn(insertSql.toString());
    });
    String s = StringUtils.substringBeforeLast(result.toString(), ",");
    return "牛逼了!!!     表"+s+".创建成功且数据已经插入";
  }

  public static void main(String[] args) {
  }
}
